package com.itboye.ihomebank.key;

import java.util.Observable;
import java.util.Observer;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.ChooseTimePopwindow;
import com.itboye.ihomebank.util.TimesUtils;
import com.itboye.ihomebank.util.ChooseTimePopwindow.onFinishedLisenter;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 发送钥匙
 * 
 * @author 余 焱
 * 
 */
public class ActivitySendKey extends BaseOtherActivity implements Observer, onFinishedLisenter {

	ImageView send_key_finish;// 返回
	RelativeLayout send_key_xuanze;// 选择钥匙类型
	TextView send_key_xuan;// 钥匙类型

	LinearLayout send_key_kuang;// 选择框

	LinearLayout send_layout01;// 永久钥匙时候的界面
	EditText send_yaoshi, send_beizhu;// 永久钥匙时候的界面(住家账号,备注)
	TextView send_ok01;// 永久钥匙时候的界面(发送)

	LinearLayout send_layout02;// 期限钥匙时候的界面
	EditText send_yaoshi02, send_beizhu02;// 期限钥匙时候的界面(住家账号,备注)
	TextView send_start_time, send_stop_time;// 期限钥匙时候的界面(开始时间，结束时间)
	TextView send_ok02;// 期限钥匙时候的界面(发送)

	PopupWindow popupWindow;
	UserPresenter userPresenter;
	private String lockId;
	private String uid;
	private String toUid;
	private String mark;
	
	long startT,endT;
	long currentTime;
	@Override
	public void onClick(View v) {
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		switch (v.getId()) {
		case R.id.send_key_finish:
			finish();
			break;
		case R.id.send_key_xuanze:
			PuPoWindow(v);
			break;
		case R.id.send_key_yongjiu:// 永久钥匙
			popupWindow.dismiss();
			send_key_xuan.setText("永久");
			send_layout01.setVisibility(View.VISIBLE);
			send_layout02.setVisibility(View.GONE);

			break;
		case R.id.send_key_qixian:// 期限钥匙
			popupWindow.dismiss();
			send_key_xuan.setText("期限");
			send_layout01.setVisibility(View.GONE);
			send_layout02.setVisibility(View.VISIBLE);
			break;
		case R.id.send_ok01:// 永久
			if(send_yaoshi.getText().toString().equals("")){
				ByAlert.alert("请输入对方账号");
			}
			showProgressDialog("请稍后...", true);
			String touid = send_yaoshi.getText().toString();
			mark = send_beizhu.getText().toString();
			lockId = getIntent().getStringExtra("lockid");
			userPresenter.sendKey(uid, touid, lockId, "0", "0", mark);
			break;
		case R.id.send_ok02:
			if(send_yaoshi02.getText().toString().equals("")){
				ByAlert.alert("请输入对方账号");
			}
			if(!send_start_time.getText().toString().equals("")&&!send_stop_time.getText().toString().equals("")){
				if (endT <= startT) {
					ByAlert.alert("结束时间应该大于开始时间");
					return;
				}
				if (currentTime > endT) {
					ByAlert.alert("结束时间应该大于当前时间");
					return;
				}
			}
			
			showProgressDialog("请稍后...", true);
			mark = send_beizhu02.getText().toString();
			String toUid = send_yaoshi02.getText().toString();
			lockId = getIntent().getStringExtra("lockid");
			userPresenter.sendKey(uid, toUid, lockId, startT/1000+"", endT/1000+"", mark);
			break;
		case R.id.send_start_time:
			ChooseTimePopwindow startTimePopwindow = new ChooseTimePopwindow(
					this, this, true);
			startTimePopwindow.showAtLocation(v, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
			break;
		case R.id.send_stop_time:
			ChooseTimePopwindow endTimePopwindow = new ChooseTimePopwindow(
					this, this, false);
			endTimePopwindow.showAtLocation(v, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
			break;
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_send_key;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		userPresenter = new UserPresenter(this);
		currentTime = System.currentTimeMillis();
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	/**
	 * 钥匙搜索切换框
	 * 
	 * @param v
	 */
	private void PuPoWindow(View v) {

		View view = LayoutInflater.from(this).inflate(
				R.layout.item_yaoshi_qiehuan, null);
		TextView send_key_yongjiu = (TextView) view
				.findViewById(R.id.send_key_yongjiu);
		TextView send_key_qixian = (TextView) view
				.findViewById(R.id.send_key_qixian);

		send_key_yongjiu.setOnClickListener(this);
		send_key_qixian.setOnClickListener(this);
		popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.showAsDropDown(v);
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.sendKey_success) {
				ByAlert.alert(entity.getData());
				finish();
			} else if (entity.getEventType() == UserPresenter.sendKey_fail) {

				ByAlert.alert(entity.getData());
			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void getBeginTime(String year, String month, String day,
			String hour, String minite) {
		// TODO Auto-generated method stub
		startT = TimesUtils.getStringToDateTwo02(year + "-" + month + "-" + day + "  " + hour);
		send_start_time.setText(year+"-"+month+"-"+day+" "+hour+":"+minite);
	}

	@Override
	public void getEndTime(String year, String month, String day, String hour,
			String minite) {
		endT = TimesUtils.getStringToDateTwo02(year + "-" + month + "-" + day + "  " + hour + ":"
				+ minite);
		send_stop_time.setText(year+"-"+month+"-"+day+" "+hour+":"+minite);
	}
}
